package com.star.security.authentication;

import com.star.security.user.UserDetails;
import lombok.Getter;
import lombok.Setter;

/**
 * 抽象身份验证
 *
 * @author yangzj
 */
@Setter
@Getter
public class AuthenticationToken implements Authentication {

    private Object principal;
    private Object credentials;
    private Long expiresIn;
    private Boolean multiEndpoint;
    private Object details;
    private boolean authenticated;

    public AuthenticationToken(Object principal, Object credentials, Object details, Long expiresIn, Boolean multiEndpoint) {
        this.principal = principal;
        this.credentials = credentials;
        this.details = details;
        this.expiresIn = expiresIn;
        this.multiEndpoint = multiEndpoint;
        // 默认未验证
        this.authenticated = false;
    }

    /**
     * 刷新用户信息
     *
     * @param principal 用户信息
     * @return {@link Authentication}
     */
    public static Authentication refresh(Object principal) {
        return new AuthenticationToken(principal, null, null, null, null);
    }

    @Override
    public boolean isAuthenticated() {
        return this.authenticated;
    }

    @Override
    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        this.authenticated = isAuthenticated;
    }

    @Override
    public String getName() {
        if (this.getPrincipal() instanceof UserDetails) {
            return ((UserDetails) this.getPrincipal()).getId().toString();
        }
        return (this.getPrincipal() == null) ? "" : this.getPrincipal().toString();
    }
}
